<template>
<div class="login-form">
  <div class="login-box">
    <!-- <img :src="bkImg" style="height: 100%; flex-shrink: 0" /> -->
    <div class="login-input">
      <!-- <img :src="logoImg" /> -->
      <span class="title">北京市新时代文明实践号管理平台</span>
      <!-- <span class="welcome">欢迎登录</span> -->
      <el-form :model="dataForm" :rules="dataRule" size="small" ref="dataForm" @keyup.enter.native="dataFormSubmit()" style="width: 356px; margin-top: 7vh;">
        <el-col :span="24">
          <el-form-item prop="mobilePhone">
            <el-input v-model="dataForm.mobilePhone" style="width: 100%;" placeholder="帐号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="password">
            <el-input v-model="dataForm.password" style="width: 100%;" type="password" placeholder="密码" show-password></el-input>
          </el-form-item>
        </el-col>
        <el-button class="login-btn-submit" style="width: 100%;margin-top:4vh" @click="dataFormSubmit()">
          登录
        </el-button>
      </el-form>
    </div>
  </div>
  <Verify ref="verify" @success="onVerifySuccess" :mode="'pop'" captchaType="blockPuzzle" :imgSize="{ width: '330px', height: '155px' }" />
</div>
</template>

<script>
import {
  SystemController
} from '@/api';
import {
  mapMutations
} from 'vuex';
import projectConfig from '@/core/config';
import {
  encrypt,
  setToken
} from '@/utils';
import Verify from '@/components/Verifition/Verify.vue';
import {
  GroundController
} from '@/api';
import {
  treeDataTranslate
} from '@/utils';
export default {
  components: {
    Verify
  },
  data() {
    return {
      bkImg: require('@/assets/img/login.png'),
      logoImg: require('@/assets/img/login_logo.png'),
      dataForm: {
        mobilePhone: 'admin',
        password: '123456'
      },
      dataRule: {
        mobilePhone: [{
          required: true,
          message: '帐号不能为空',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '密码不能为空',
          trigger: 'blur'
        }]
      },
      projectName: projectConfig.projectName
    };
  },
  methods: {
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$refs.verify.show();
        }
      });
    },
    getAreaList() {
      let prarms2 = ['province', 'city'];
      let prarms3 = ['province', 'city', 'district'];
      let prarms4 = ['province', 'city', 'district', 'street'];
      GroundController.getArea(this, {
        levels: prarms2
      }).then(res => {
        let treeData = treeDataTranslate(res.data, 'code', 'parentId');
        sessionStorage.setItem('areaOptions2', JSON.stringify(treeData));
      })
      GroundController.getArea(this, {
        levels: prarms3
      }).then(res => {
        let treeData = treeDataTranslate(res.data, 'code', 'parentId');
        sessionStorage.setItem('areaOptions3', JSON.stringify(treeData));
      })
      GroundController.getArea(this, {
        levels: prarms4
      }).then(res => {
        let treeData = treeDataTranslate(res.data, 'code', 'parentId');
        sessionStorage.setItem('areaOptions4', JSON.stringify(treeData));
      })
    },
    login(verifyParams) {
      let params = {
        loginName: this.dataForm.mobilePhone,
        password: encrypt(this.dataForm.password),
        captchaVerification: (verifyParams || {}).captchaVerification
      };
      setToken(null);
      SystemController.login(this, params, null, {
        showMask: false
      }).then(data => {
        this.setMenuList(data.data.menuList);
        delete data.data.menuList;
        this.getAreaList()
        this.setUserInfo(data.data);
        setToken(data.data.tokenData);
        this.setCurrentMenuId(null);
        this.$router.replace({
          name: 'main'
        });
        // this.$router.go(-1)
      }).catch(e => {});
    },
    onVerifySuccess(verifyParams) {
      this.login(verifyParams);
    },
    ...mapMutations(['setUserInfo', 'setMenuList', 'setCurrentMenuId'])
  },
  mounted() {
    this.setMenuList([]);
    this.setUserInfo({});
    setToken();
  }
};
</script>

<style lang="scss">
.login-form {
    width: 100vw;
    height: 100vh;
    background-image: url("../../assets/img/login.jpg");
    background-size: cover;
    // background: #292D36;
    display: flex;
    justify-content: center;
    align-items: center;
    .login-box {
        display: flex;
        align-items: center;
        height: 58vh;
        padding: 6vh;
        border-radius: 10px;
        background: white;
        .login-input {
            height: 100%;
            width: 410px;
            display: flex;
            align-items: center;
            flex-direction: column;
            img {
                width: 356px;
                height: 42px;
                margin-top: 6vh;
            }
            .title {
                color: #232323;
                font-size: 30px;
                margin-top: 2vh;
                font-weight: bolder;
                text-align: center
            }
            .welcome {
                margin-top: 1vh;
                font-size: 18px;
            }
        }
    }
}

.login-form .el-input__inner {
    height: 40px!important;

}

.login-form .el-button {
    height: 40px!important;
    line-height: 15px;
    font-size: 17px;
}
.login-btn-submit {
    margin-top: 2vh;
}
</style>
